package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyPassKindBean;
import flc.ast.bean.MyPassRecordBean;
import flc.ast.databinding.ActivityAddPasswordBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.utils.SPUtil;

public class AddPasswordActivity extends BaseAc<ActivityAddPasswordBinding> {

    public static MyPassKindBean kindBean;
    private boolean isPasswordVisible = false;


    @Override
    protected int onCreate() {
        return R.layout.activity_add_password;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddPasswordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvType.setOnClickListener(this);
        mDataBinding.ivPasswordShow.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvType://类型
                startActivity(TypeActivity.class);
                break;
            case R.id.ivPasswordShow://密码显示隐藏
                togglePasswordVisibility();
                break;
            case R.id.tvConfirm://确定
                addRecord();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mDataBinding.ivImg.setImageResource(kindBean.getImg());
        mDataBinding.tvType.setText(kindBean.getKind());
    }

    private void togglePasswordVisibility() {
        if (isPasswordVisible) {
            // 隐藏密码
            mDataBinding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            mDataBinding.ivPasswordShow.setImageResource(R.drawable.bkj); // 切换为不可见图标的资源
        } else {
            // 显示密码
            mDataBinding.etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            mDataBinding.ivPasswordShow.setImageResource(R.drawable.kj); // 切换为可见图标的资源
        }

        isPasswordVisible = !isPasswordVisible;
        // 强制光标更新，以确保密码的可见性立即更改
        mDataBinding.etPassword.setSelection(mDataBinding.etPassword.getText().length());
    }

    //添加新记录
    private void addRecord() {
        String name = mDataBinding.etName.getText().toString();
        String account = mDataBinding.etAccount.getText().toString();
        String password = mDataBinding.etPassword.getText().toString();
        String remakes = mDataBinding.etRemark.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort(R.string.please_input_pass_name);
        } else if (TextUtils.isEmpty(account)) {
            ToastUtils.showShort(R.string.please_input_account_name);
        } else if (TextUtils.isEmpty(password)) {
            ToastUtils.showShort(R.string.please_input_password);
        } else {
            //准备数据
            List<MyPassRecordBean> listSave = new ArrayList<>();
            if (TextUtils.isEmpty(remakes)) {
                remakes = getString(R.string.no_remakes);
            }
            listSave.add(new MyPassRecordBean(TimeUtils.getNowString(), name, account, password, remakes));
            //保存到记录
            saveRecord(listSave);
        }
    }

    //保存到记录
    private void saveRecord(List<MyPassRecordBean> list) {
        List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
        if (listSp != null && listSp.size() > 0) {
            for (MyPassKindBean bean : listSp) {
                if (bean.getKind().equals(kindBean.getKind())) {
                    if (bean.getList() != null && bean.getList().size() > 0) {
                        bean.getList().addAll(list);
                    } else {
                        bean.setList(list);
                    }
                }
            }
            AppSpUtils.savePasswordList(listSp);
            //总条数+1
            int countPass = SPUtil.getInt(mContext, Content.countPass, 0);
            countPass++;
            SPUtil.putInt(mContext, Content.countPass, countPass);
            ToastUtils.showShort(R.string.add_suc);
            finish();
        }
    }

}